package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }

    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);

        //设置分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page = setmealMapper.selectPage(page, wrapper);

        //遍历获取某个套餐
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtils.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据setmeal中的category_id从category表中查询name
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                //根据setmeal的id从setmeal_dish表中查询列表
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);

                setmeal.setSetmealDishes(setmealDishList);
            }
        }

        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //保存基本信息到setmeal表中
        setmealMapper.insert(setmeal);

        //获取setmeal中的setmealDish，保存到中间表中
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                //执行保存
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //判断套餐是否还在销售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("要删除的套餐还在售，无法删除");
        }

        //先删除表中所有符合条件的数据
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        //再删除套餐菜品中间表中所有符合条件的数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper1);
    }

    @Override
    public void updateByIds(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        //根据id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //根据套餐id查询菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //根据主键修改菜品表中的信息
        setmealMapper.updateById(setmeal);

        //根据套餐id删除中间表中相关信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //获取setmeal中的setmealDish，保存到中间表中
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtils.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                //执行保存
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public List<Dish> getMealId(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        ArrayList<Dish> dishArrayList = new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishList) {
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());
            dishArrayList.add(dish);
        }
        return dishArrayList;
    }
}
